//
//  DiscoverTableVC.m
//  BiliBiliDemo
//
//  Created by XDH on 16/4/12.
//  Copyright © 2016年 XDH. All rights reserved.
//

#import "DiscoverTableVC.h"
#import "WebViewVC.h"
#import "GameCenterCollectionViewController.h"
@interface DiscoverTableVC ()
@property (weak , nonatomic) UIView *headV;


@end
@implementation DiscoverTableVC
-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //隐藏导航栏
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
   //添加tableView的headView
    UIView *headView  = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width,200 )];
    headView.backgroundColor = [UIColor blueColor];
    self.headV = headView;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    UIButton *btn = [[UIButton alloc] init];
    btn.backgroundColor = [UIColor orangeColor];
    btn.frame = CGRectMake(50, 50, 200, 20);
    [btn setTitle:@"点我放大缩小" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnCLick) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:btn];
    
    self.tableView.tableHeaderView = headView;
    
    
    //设置tableView的footView
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 1)];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 1 || section == 2 ) return 2;
    return 1;
}

static NSString * const ID = @"discover";
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    cell.textLabel.textColor = [UIColor colorWithWhite:0.1 alpha:0.9];
    cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"jiantou_you"]];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor colorWithRed:235/255.0 green:235/255.0 blue:241/255.0 alpha:1];
    view.bounds = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width - 40, 1);
    view.frame = CGRectMake(20, 0, [UIScreen mainScreen].bounds.size.width - 20, 1);
    
    //设置cell选中还是无色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.section == 0) {
        cell.textLabel.text  = @"兴趣圈";
        cell.imageView.image = [UIImage imageNamed:@"xingququan"];
    }
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            cell.textLabel.text  = @"原创排行榜";
            cell.imageView.image = [UIImage imageNamed:@"yuanchuangpaihangbang"];
        } else {
        [cell.contentView addSubview:view];
        cell.textLabel.text  = @"全区排行榜";
        cell.imageView.image = [UIImage imageNamed:@"quanqupaihangbang"];
        }
    }
    if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            cell.textLabel.text  = @"游戏中心";
            cell.imageView.image = [UIImage imageNamed:@"youxizhongxing"];
        } else {
            [cell.contentView addSubview:view];
            cell.textLabel.text  = @"海外游";
            cell.imageView.image = [UIImage imageNamed:@"haiwaiyou"];
        }

    }
    
 
    return cell;
}
//设置section高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}
#pragma mark -设置Section间距
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.1;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width,1 )];
    view.backgroundColor = [UIColor clearColor];
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 8;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width,1 )];
    view.backgroundColor = [UIColor clearColor];
    return view;
    
}

#pragma mark - 按钮点击,调整headerView的高度
-(void)btnCLick {
    
    CGRect frame = self.headV.frame;
    frame.size.height =  (frame.size.height == 200) ? 300 : 200;
    self.headV.frame = frame;
    self.tableView.tableHeaderView = self.headV;
    
}


#pragma mark - 跳转控制器
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 2 && indexPath.row == 1) {
        WebViewVC *VC = [[WebViewVC alloc] init];
//        VC.view.backgroundColor = [UIColor colorWithRed:235/255.0 green:235/255.0 blue:241/255.0 alpha:1];
        VC.view.backgroundColor = [UIColor lightGrayColor];
        VC.title = @"http://yoo.bilibili.com/html/indexm.html";
        [self.navigationController pushViewController:VC animated:YES];
    }
    if (indexPath.section == 2 && indexPath.row == 0) {
        GameCenterCollectionViewController *VC = [[GameCenterCollectionViewController alloc] init];
        VC.view.backgroundColor = [UIColor lightGrayColor];
        VC.title = @"游戏中心";
        [self.navigationController pushViewController:VC animated:YES];
    }
    
}
@end
